股评师分析报告文本情感分析预测股价
github有一个Apple股价预测项目,https://github.com/Twabeeric/AAPLTextMining
股评师分析报告是否会对股价产生影响,为此本文使用文本情感分析对股评师报告对文本数据进行了度量。
项目数据包括:
Apple 2017-2018股价数据
ThomsonOne网站Sherri Scribner(德意志银行股评师)对Apple公司周度研究报告(2017-2018),有65个docx文件
导入数据
我们需要按照日期,将报告与股价数据按照日期合并到一起, 我们先导入分析师报告,一共有65个docx文件
from io import StringIO
import pandas as pd
import datetime
import re
import docx
import os
def read_from_docx(file_path):
"""
读取docx文件,并返回其中的文本内容
:param file_path: docx文件路径
:return: docx中的文本内容
"""
texts = ''
doc = docx.Document(file_path)
for para in doc.paragraphs:
texts += para.text
return texts
flist = [f for f in os.listdir('data/reports') if 'docx' in f]
Descriptions = [read_from_docx('data/reports/'+f) for f in flist]
Dates = [f[:8] for f in flist]
data1 = pd.DataFrame({'Date': Dates,
'Descriptions': Descriptions})
data1.head()
将日期Date整理为datetime类型
data1['Date'] = pd.to_datetime(data1['Date'])
data1.sort_values('Date', inplace=True)
data1.head()
导入Apple公司股价数据
data2= pd.read_csv('data/AAPL.csv')
data2['Date'] = pd.to_datetime(data2['Date'])
data2.head()
将股价和分析师报告按照Date合并
df = pd.merge(data1, data2)
df.head()
文本情感计算
pysentiment库对金融文本数据进行情感计算。查了下该库的官方文档,发现该库提供了两大情感分析
Harvard IV-4 英文通用情感分析,喜怒哀乐,主要是形容词情感词典
Loughran&MCdonald 英文金融情感分析,金融领域特有的情感词典
该库的教程可见于 http://bit.ly/2LECfw5
使用pandas的apply批处理方法,可以批量对对某一列施行某种算法(计算函数)
from pysentiment import lm,hiv4
lm = lm.LM()
hiv4 = hiv4.HIV4()
def hiv4_positive(text):
words = hiv4.tokenize(text)
score = hiv4.get_score(words)
return score['Positive']
def hiv4_negative(text):
words = hiv4.tokenize(text)
score = hiv4.get_score(words)
return score['Negative']
def hiv4_polarity(text):
words = hiv4.tokenize(text)
score = hiv4.get_score(words)
return score['Polarity']
def hiv4_subjectivity(text):
words = hiv4.tokenize(text)
score = hiv4.get_score(words)
return score['Subjectivity']
def lm_positive(text):
words = lm.tokenize(text)
score = lm.get_score(words)
return score['Positive']
def lm_negative(text):
words = lm.tokenize(text)
score = lm.get_score(words)
return score['Negative']
def lm_polarity(text):
words = lm.tokenize(text)
score = lm.get_score(words)
return score['Polarity']
def lm_subjectivity(text):
words = lm.tokenize(text)
score = lm.get_score(words)
return score['Subjectivity']
df['hiv4_pos'] = df['Descriptions'].apply(hiv4_positive)
df['hiv4_neg'] = df['Descriptions'].apply(hiv4_negative)
df['hiv4_polarity'] = df['Descriptions'].apply(hiv4_polarity)
df['hiv4_subjectivity'] = df['Descriptions'].apply(hiv4_subjectivity)
df['lm_pos'] = df['Descriptions'].apply(lm_positive)
df['lm_neg'] = df['Descriptions'].apply(lm_negative)
df['lm_polarity'] = df['Descriptions'].apply(lm_polarity)
df['lm_subjectivity'] = df['Descriptions'].apply(lm_subjectivity)
df.head()
可视化
查看股价与报告的情感得分走势关系
import matplotlib.pyplot as plt
import seaborn as sns
f, axes = plt.subplots(nrows=2, ncols=1, figsize=(14, 6))
axes[0].set_title('Stock Price、HIV4_polarity & LM_polarity')
sns.lineplot(x="Date", y="Adj Close", data = df, label='Price', ax=axes[0]);
sns.lineplot(x="Date", y="hiv4_polarity", data = df, label='hiv4_polarity', ax=axes[1]);
sns.lineplot(x="Date", y="lm_polarity", data = df, label='lm_polarity', ax=axes[1]);
本文只是找了很少的数据,从图中走势看,LM金融情感极性得分(lm_polarity)与股价price走势更相似。说明对于苹果公司而言,通过对股评师有关苹果公司的报告进行文本情感计算,能够一定程度上预测股价走势。
近期文章
课件获取方式,请在公众号后台回复关键词“20191211”
觉得本文有用,请不吝点赞评论转发~谢谢支持~